import Vue from 'vue'
import VueRouter from 'vue-router'
import {getToken, getUserId} from "../utils/auth";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

const Index = () => import('../views/Index')
const Login = () => import('../views/Login')
const Layout = () => import('../views/Layout')
const BlogList = () => import('../views/blog/BlogList')
const BlogPublish = () => import('../views/blog/BlogPublish')
const TagList = () => import('../views/tag/TagList')
const TypeList = () => import('../views/type/TypeList')
const TypePublish = () => import('../views/type/TypePublish')
const TagPublish = () => import('../views/tag/TagPublish')
const CommentList = () => import('../views/comment/CommentList')

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {title: '登录'}
  },
  {
    path: '',
    redirect: '/index',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Index',
        component: Index,
        meta: {title: '首页'}
      }
    ]
  },
  {
    path: '/blog',
    name: 'Blog',
    redirect: '/blog/blogList',
    component: Layout,
    children: [
      {
        path: 'blogList',
        component: BlogList,
        name: 'blogList',
        meta: {title: '博客列表'}
      },
      {
        path: 'blogPublish',
        component: BlogPublish,
        name: 'blogPublish',
        meta: {title: '发布博客'}
      },
    ]
  },
  {
    path: '/tag',
    name: 'Tag',
    redirect: '/tag/tagList',
    component: Layout,
    children: [
      {
        path: 'tagList',
        component: TagList,
        name: 'tagList',
        meta: {title: '标签列表'}
      },
      {
        path: 'tagPublish',
        component: TagPublish,
        name: 'tagPublish',
        meta: {title: '新增标签'}
      },
    ]
  },
  {
    path: '/type',
    name: 'Type',
    redirect: '/type/typeList',
    component: Layout,
    children: [
      {
        path: 'typeList',
        component: TypeList,
        name: 'typeList',
        meta: {title: '分类列表'}
      },
      {
        path: 'typePublish',
        component: TypePublish,
        name: 'typePublish',
        meta: {title: '新增分类'}
      },
    ]
  },
  {
    path: '/comment',
    component: Layout,
    name: 'Comment',
    redirect: '/comment/:id',
    children: [
      {
        path: '/:id',
        component: CommentList,
        name: 'CommentList',
        meta: {title:'评论列表'}
      }
    ]
  },
]

const router = new VueRouter({
  base: '/blog_admin/',
  routes,
  mode: 'history',
  linkActiveClass: "active",
})

NProgress.configure({ showSpinner: false })

/* 不重定向白名单 */
const whiteList = ['/login',]
/*路由跳转前调用*/
router.beforeEach((to, from, next) => {
  NProgress.start()
  document.title = to.meta.title;
  if (getToken()&&getUserId()) {
    // 有Token

    next()
  } else {
    // 没有Token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
